#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdbool.h>
typedef struct ListNode ListNode;
struct ListNode
{
    int val;
    struct ListNode* next;
};
struct ListNode* detectCycle(struct ListNode* head)
{
    ListNode* slow = head;
    ListNode* fast = head;
    ListNode* tmp = head;
    while (fast && fast->next)
    {
        slow = slow->next;
        fast = fast->next->next;
        if (slow == fast)
        {
            break;
        }
    }
    if (fast == NULL || fast->next == NULL)
    {
        return NULL;
    }
    else
    {
        while (slow != tmp)
        {
            slow = slow->next;
            tmp = tmp->next;
        }
        return tmp;
    }
}